import { reqDetailList } from '@/api/index';
import { reqAddOrUpdateCart } from '@/api/index'
import { getUUID } from '@/utils/user_uuid'

let state = {
    detailInfo: {},
    uuid_token:getUUID(),
};

let mutations = {
    GETDETAILINFO(state, detailInfo) {
        state.detailInfo = detailInfo;
    }
};

let actions = {
    async getDetailInfo({ commit }, skuid) {
        let result = await reqDetailList(skuid);
        if (result.code == 200) {
            commit('GETDETAILINFO', result.data);
        }
    },
    
    /* eslint-disable */
    async addOrUpdateCart({ commit }, {skuId,skuNum}) {
        let result = await reqAddOrUpdateCart(skuId,skuNum);
        if (result.code == 200) {
            //如果加入购物车成功,返回promise即为成功
            return "ok";
       } else {
            //如果加入购物车失败，返回失败的Promise
            return Promise.reject();
       }
    }
    /* eslint-disable */
};

let getters = {
    categoryView(){
        return state.detailInfo.categoryView||{}
    },
    skuInfo(){
        return state.detailInfo.skuInfo||{}
    },
    spuSaleAttrList(){
        return state.detailInfo.spuSaleAttrList||[]
    }
};

export default {
    state,
    mutations,
    actions,
    getters
}